
import {createSlice} from '@reduxjs/toolkit'
import {reqGetCategory1ListData,reqGetCategory2ListData,reqGetCategory3ListData,reqGetCateGoryby3Id} from '@/api/attr'
import {useSelector} from 'react-redux'
const attrInfoSlice = createSlice({
    name:'attr',
    initialState:{
        category1Id:'',
        category2Id:'',
        category3Id:'',
        attrInfo:{},
        category1List:[],
        category2List:[],
        category3List:[],
        categoryList:[]
    },
    reducers:{
        UpCategory1id(state,{payload}){
            state.category1Id=payload
            state.category2Id=''
            state.category3Id=''
        },
        UpCategory2id(state,{payload}){
            state.category2Id=payload
            state.category3Id=''
        },
        UpCategory3id(state,{payload}){
            state.category3Id=payload
        },
        UpCategoryList1(state,{payload}){
            state.category1List = payload
        },
        UpCategoryList2(state,{payload}){
            state.category2List = payload
        },
        UpCategoryList3(state,{payload}){
            state.category3List = payload
            console.log(852456)
        },
        UpCategorylist(state,{payload}){
            state.categoryList = payload
        }
    }
})

const {UpCategorylist,UpCategory1id,UpCategory2id,UpCategory3id,UpCategoryList1,UpCategoryList2,UpCategoryList3} = attrInfoSlice.actions
// export const useSelectorTrademark=()=>useSelector((state:any)=>state.trademark)
export const useSelectorAttrLlist=()=>useSelector((state:any)=>state.attr)

export const getCateGory1Sync=()=>{
    return async (dispatch:any)=>{
        const result = await reqGetCategory1ListData()
        console.log(result)
        dispatch(UpCategoryList1(result.data))
    }
}
export const getCateGory2Sync=(category1Id:number)=>{
    return async (dispatch:any)=>{
        const result = await reqGetCategory2ListData(category1Id)
        console.log(result)
        dispatch(UpCategoryList2(result.data))
    }
}
export const getCateGory3Sync=(category2Id:number)=>{
    return async (dispatch:any)=>{
        const result = await reqGetCategory3ListData(category2Id)
        console.log(result)
        dispatch(UpCategoryList3(result.data))
    }
}
export const changeCate1Id=(category1Id:number)=>{
    return async (dispatch:any)=>{
        dispatch(UpCategory1id(category1Id))
        dispatch(getCateGory2Sync(category1Id))
    }
}
export const changeCate2Id=(category2Id:number)=>{
    return async (dispatch:any)=>{
        dispatch(UpCategory2id(category2Id))
        dispatch(getCateGory3Sync(category2Id))
    }
}
export const changeCate3Id=(category1Id:number,category2Id:number,category3Id:number)=>{
    return async (dispatch:any)=>{
        dispatch(UpCategory3id(category3Id))
        const result = await reqGetCateGoryby3Id(category1Id,category2Id,category3Id)
        console.log(result)
        dispatch(UpCategorylist(result))
    }
}
export default attrInfoSlice.reducer